package day9Exer;
/*
* 使用匿名内部类对象求图形面积
*
* */
public class Demo2 {
    public static void main(String[] args) {

        //矩形的匿名内部类对象
        Shape rectangle = new Shape(){

            @Override
            public double getArea(){
                return getBase()*getHeight();
            }
        };

        //给对象的属性赋值
        rectangle.setBase(10);
        rectangle.setHeight(20);
        System.out.println("矩形面积为:"+rectangle.getArea());

        //三角形的匿名内部类
        Shape triangle = new Shape() {
            @Override
            public double getArea() {
                return getBase()*getHeight()/2;
            }
        };

        //给对象的属性赋值
        triangle.setBase(10);
        triangle.setHeight(20);
        System.out.println("三角形面积为:"+triangle.getArea());

    }
}

abstract class Shape{
    //底边长
    private double base;

    //高
    private double height;

    public void setBase(double base) {
        this.base = base;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    public double getBase() {
        return base;
    }

    public double getHeight() {
        return height;
    }

    public abstract double getArea();
}
